#pragma once

#include "../util/except.hh"
#include "../poco/word.hh"
#include "../repository/word-repository.hh"

namespace WordMemory
{
    class WordService
    {
    public:
        static WordService &instance()
        {
            static WordService inst;
            return inst;
        }

        // 返回包含单词信息和收藏状态的
        WordWithFavoriteStatus getWordWithFavoriteStatus(int user_id, const std::string &word_text)
        {
            try
            {
                auto pair = WordRepository::instance().getWordAndFavoriteStatus(user_id, word_text);
                return WordWithFavoriteStatus(pair.first, pair.second);
            }
            catch (const RepositoryExcept& e)
            {
                spdlog::error("WordService: Error in getWordWithFavoriteStatus for word '{}', user {}: {}", word_text, user_id, e.what());
                throw;
            }
        }


        Word getWordByUserId(int user_id, const std::string &word)
        {
            return WordRepository::instance().getWordAndFavoriteStatus(user_id, word).first;
        }

        void addExample(int word_id, const std::string &example, const std::string &translation)
        {
            if (example.empty() || translation.empty())
            {
                throw ServerExcept("Example or translation cannot be empty");
            }
            WordRepository::instance().addExample(word_id, example, translation);
        }

        void addMnemonic(int word_id, const std::string &mnemonic)
        {
            if (mnemonic.empty())
            {
                throw ServerExcept("Mnemonic cannot be empty");
            }
            WordRepository::instance().addMnemonic(word_id, mnemonic);
        }

        void updateStatus(int user_id, int word_id, const std::string &status)
        {
            if (status != "forgot" && status != "vague" && status != "known")
            {
                throw ServerExcept("Invalid status");
            }
            WordRepository::instance().updateStatus(user_id, word_id, status);
        }

    private:
        WordService() = default;
        WordService(const WordService &) = delete;
        WordService &operator=(const WordService &) = delete;
    };
}